package cst.neu.yuejian_31.model;

import java.util.List;

import cst.neu.yuejian_31.bean.ClubBean;
import cst.neu.yuejian_31.iface.ClubSearchIface;
import cst.neu.yuejian_31.iface.ClubSearchListener;
import cst.neu.yuejian_31.service.ClubSearchService;
import cst.neu.yuejian_31.Common.Constant;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

/**
 * Created by xxe on 2017/11/2.
 */

public class ClubSearchModel implements ClubSearchIface {
    private Retrofit retrofit;
    public ClubSearchModel(){
        retrofit=new Retrofit.Builder()
                .baseUrl(Constant.BASE_URL)
                .addConverterFactory(GsonConverterFactory.create())
                .build();
    }
    @Override
    public void searchClub(String clubName, final ClubSearchListener clubSearchListener) {
        ClubSearchService clubSearchService=retrofit.create(ClubSearchService.class);
        Call<List<ClubBean>>call=
                clubSearchService.searchClub(clubName);
        call.enqueue(new Callback<List<ClubBean>>() {
            @Override
            public void onResponse(Call<List<ClubBean>> call, Response<List<ClubBean>> response) {
                if(response.isSuccessful()&&response.body()!=null){
                    clubSearchListener.onSuccess(response.body());
                }else {
                    clubSearchListener.onFail("Fail");
                }
            }

            @Override
            public void onFailure(Call<List<ClubBean>> call, Throwable t) {
                clubSearchListener.onFail(t.toString());
            }
        });

    }
}
